package Terminal.View;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.border.Border;

import Terminal.View.Gestori.GestoreInterfaccia;
import Terminal.View.Gestori.GestoreTimeRecord;

public class JElenco extends JPanel {

	private static final long serialVersionUID = 1L;
	protected JList<String> lista;
	protected DefaultListModel<String> listmodel;
	protected JScrollPane listScrollPane;
	protected ArrayList<String> arrayelementi = new ArrayList<String>();
	protected GestoreTimeRecord mediator = GestoreTimeRecord.istanza();
	
	public JElenco()
	{
		super();
		this.createComponents();
	}
	
	public JElenco( ArrayList<String> stringhe) {
		super();
		this.arrayelementi = stringhe;
		this.createComponents();
	}

	private void createComponents() {
		
		this.setBounds(20,231, 350, 350);
		this.setLayout(new BorderLayout());
		
		this.setJListModel();
		this.setJList();
		this.setScrollPane();
		
		this.add(listScrollPane, BorderLayout.CENTER);
		
		this.setVisible(true);
	}

	private void setJListModel() {
		
		listmodel = new DefaultListModel<String>();
		
		int i=1;
		if(arrayelementi!=null)
		{
			for(String pr:arrayelementi)
			{
				listmodel.addElement(i+":"+ pr);
				i++;
			}
		}
		else{listmodel.addElement(" ");}
		
	}	
	
	private void setJList() {
				
		lista = new JList<String>(listmodel);
		
		lista.setBounds(0, 0, 350, 350);
		
		lista.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		lista.setSelectedIndex(0);
		lista.setLayoutOrientation(0);
		lista.setVisibleRowCount(10);
		lista.setFont(new Font(lista.getFont().getName(), Font.PLAIN,18));
		lista.setBorder( BorderFactory.createLineBorder(Color.gray) );
		
		
	}

	private void setScrollPane() {
		
		listScrollPane = new JScrollPane(lista);
		listScrollPane.setBounds(20, 80, 350, 350);
		listScrollPane.setPreferredSize(new Dimension(350,350));
		listScrollPane.setBorder( BorderFactory.createLineBorder(Color.gray) );	
		
	}
	
	public void setElementi(ArrayList<String> stringhe)
	{
		int i=1;
		if(arrayelementi!=null)
		{
			for(String pr:arrayelementi)
			{
				listmodel.addElement(i+":"+ pr);
				i++;
			}
		}
		else{listmodel.addElement(" ");}		
	}
	
	public int getSelectedIndex(){
		return lista.getSelectedIndex();
	}
	
	
	public void setDisabilitaLista(boolean bool){
		lista.setEnabled(bool);
		validate();
		repaint();
	}

}
